package com.wjjun.base.ui.base;

import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.blankj.utilcode.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.luck.picture.lib.rxbus2.RxBus;
import com.qingmi.farmer.BuildConfig;
import com.qingmi.farmer.R;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.umeng.message.PushAgent;
import com.wjjun.basemvpvm.view.DataBindingActivity;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;
import com.wjjun.customview.window.LoadingDialog;

import javax.inject.Inject;

import dagger.android.AndroidInjection;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.support.HasSupportFragmentInjector;

/**
 * author Wjjun
 * Created 2018/3/6.
 */

public abstract class BaseActivity<VM extends BaseViewModel, VDB extends ViewDataBinding> extends DataBindingActivity<VM, VDB>
        implements BaseActivityPresenter, HasSupportFragmentInjector {

    @Inject
    public DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;

    protected RxPermissions rxPermissions;

    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (isFullScreen()) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        if (BuildConfig.DEBUG) {
            AndroidInjection.inject(this);
        } else {
            try {
                AndroidInjection.inject(this);
            } catch (Exception e) {
            }
        }
        super.onCreate(savedInstanceState);

        initImmersionBar();

        rxPermissions = new RxPermissions(this);

        initContent();
        initData();

        //统计应用启动数据,如果不调用此方法，不仅会导致按照”几天不活跃”条件来推送失效，还将导致广播发送不成功以及设备描述红色等问题发生
        PushAgent.getInstance(this).onAppStart();
    }

    protected void initImmersionBar() {
        try {
            ImmersionBar.with(this).titleBar(R.id.app_bar).keyboardEnable(true).init(); //初始化，默认透明状态栏和黑色导航栏
        } catch (Exception e) {
            //避免全屏的界面报错
            ImmersionBar.with(this).keyboardEnable(true).init();
        }
    }


    public boolean isFullScreen() {
        return false;
    }

    /**
     * 初始化控件
     */
    protected void initContent() {

    }

    /**
     * 初始化数据，发起网络请求等操作
     */
    protected void initData() {

    }

    @Override
    public void onToolBarLeftClick() {
        finish();
    }

    @Override
    public void onToolBarTitleClick() {

    }

    @Override
    public void onToolBarFirstRightBtnClick() {

    }

    @Override
    public void onToolBarSecondRightBtnClick() {

    }

    @Override
    public void onToolBarThreeRightBtnClick() {

    }


    @Override
    public void onToolBarRightTxtClick() {

    }

    @Override
    public void onClick(View view) {
    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    public AndroidInjector<Fragment> supportFragmentInjector() {
        return fragmentDispatchingAndroidInjector;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
        ImmersionBar.with(this).destroy();
        RxBus.getDefault().unregister(this);
        dismissLoadingDialog();
    }


    public void showToast(int resId) {
        if (resId != 0) {
            ToastUtils.showShort(resId);
        }
    }

    public void showToast(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            ToastUtils.showShort(msg);
        }
    }

    public void showCancelLoadingDialog(int msgId) {
        initLoadingDialog();
        loadingDialog.setTitleByRes(msgId);
        loadingDialog.setCancelable(true);
        loadingDialog.show();
    }

    public void showCancelLoadingDialog(String msg) {
        initLoadingDialog();
        loadingDialog.setTitle(msg);
        loadingDialog.setCancelable(true);
        loadingDialog.show();
    }

    public void showLoadingDialog(String msg) {
        initLoadingDialog();
        loadingDialog.setTitle(msg);
        loadingDialog.setCancelable(false);
        loadingDialog.show();
    }

    public void showLoadingDialog(int msgId) {
        initLoadingDialog();
        loadingDialog.setTitleByRes(msgId);
        loadingDialog.setCancelable(false);
        loadingDialog.show();
    }

    private void initLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
        }
    }

    public void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    public RxPermissions getRxPermissions() {
        return rxPermissions;
    }
}
